package com.rockville.ufone.rssdata;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;

import org.apache.log4j.Logger;

import com.rockville.ufone.DBProc;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndImage;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

public class HoroscopeParser implements Runnable {

	private static final Logger log = Logger.getLogger(HoroscopeParser.class);
	private static HashMap<String, String> hmParams = new HashMap<String, String>();
	static {		
		hmParams.put("Aries", "http://shine.yahoo.com/rss/astrology/aries/daily-overview/");
		hmParams.put("Taurus", "http://shine.yahoo.com/rss/astrology/taurus/daily-overview/");
		hmParams.put("Gemini", "http://shine.yahoo.com/rss/astrology/gemini/daily-overview/");
		hmParams.put("Cancer", "http://shine.yahoo.com/rss/astrology/cancer/daily-overview/");
		hmParams.put("Leo", "http://shine.yahoo.com/rss/astrology/leo/daily-overview/");
		hmParams.put("Virgo", "http://shine.yahoo.com/rss/astrology/virgo/daily-overview/");
		hmParams.put("Libra", "http://shine.yahoo.com/rss/astrology/libra/daily-overview/");
		hmParams.put("Scorpio", "http://shine.yahoo.com/rss/astrology/scorpio/daily-overview/");
		hmParams.put("Sagittarius", "http://shine.yahoo.com/rss/astrology/sagittarius/daily-overview/");
		hmParams.put("Capricorn", "http://shine.yahoo.com/rss/astrology/capricorn/daily-overview/");
		hmParams.put("Aquarius", "http://shine.yahoo.com/rss/astrology/aquarius/daily-overview/");
		hmParams.put("Pisces", "http://shine.yahoo.com/rss/astrology/pisces/daily-overview/");		
		
		hmParams.put("love-Aries", "http://shine.yahoo.com/rss/astrology/aries/daily-love/");
		hmParams.put("love-Taurus", "http://shine.yahoo.com/rss/astrology/taurus/daily-love/");
		hmParams.put("love-Gemini", "http://shine.yahoo.com/rss/astrology/gemini/daily-love/");
		hmParams.put("love-Cancer", "http://shine.yahoo.com/rss/astrology/cancer/daily-love/");
		hmParams.put("love-Leo", "http://shine.yahoo.com/rss/astrology/leo/daily-love/");
		hmParams.put("love-Virgo", "http://shine.yahoo.com/rss/astrology/virgo/daily-love/");
		hmParams.put("love-Libra", "http://shine.yahoo.com/rss/astrology/libra/daily-love/");
		hmParams.put("love-Scorpio", "http://shine.yahoo.com/rss/astrology/scorpio/daily-love/");
		hmParams.put("love-Sagittarius", "http://shine.yahoo.com/rss/astrology/sagittarius/daily-love/");
		hmParams.put("love-Capricorn", "http://shine.yahoo.com/rss/astrology/capricorn/daily-love/");
		hmParams.put("love-Aquarius", "http://shine.yahoo.com/rss/astrology/aquarius/daily-love/");
		hmParams.put("love-Pisces", "http://shine.yahoo.com/rss/astrology/pisces/daily-love/");
	}
	
	@Override
	public void run() {
		log.info("** HoroscopeParser STARTED **");
		try {
			DBProc db = DBProc.getInstance();		    
			SyndEntryImpl sei = null;
			SyndContentImpl sci = null;
			SyndFeedInput input = new SyndFeedInput();
			
			for(String star : hmParams.keySet()) {
				String url = hmParams.get(star);
				SyndFeed synfeed;
			    try {
			    	String strImage = "";
					synfeed = input.build(new XmlReader(new URL(url)));
					
	//				WireFeed wf = synfeed.createWireFeed();
	//				SyndFeedImpl sfi = new SyndFeedImpl(wf);
					SyndImage syndImg = synfeed.getImage();
					if(syndImg != null)
						strImage = "<img src=\""+syndImg.getUrl()+"\" alt=\""+star+"\" border=\"0\" style=\"margin-right:10px;\"/>";
	//				sei = new SyndEntryImpl();
					// sdformatter = new SimpleDateFormat("EEE MMM dd, yyyy HH:mm:ss");
	//				log.debug("sfi Title" + sfi.getTitle());
	//				log.debug("sfi Title" + sfi.getDescription());
	//				log.debug("sfi PublishDate " + sfi.getPublishedDate());
		            StringBuffer sb = new StringBuffer("<div style=\"text-align:justify;\">");
		            sb.append(strImage);
		            sb.append("<i>"+ synfeed.getPublishedDate() +"</i><br />");
		            
		            List lstFeed = synfeed.getEntries();
					ListIterator it = lstFeed.listIterator();
					while (it.hasNext()) {
						sei = (SyndEntryImpl) it.next();
						sci = (SyndContentImpl) sei.getDescription();
						String text = parseHoroscope(sci.getValue());
			            sb.append("<div style=\"padding-bottom:5px; border-bottom:1px solid #000000;\">"+text+"</div>");
					}
		            sb.append("</div>");
					db.updateHoroscope(star, sb.toString());					

			    } catch (Exception e) {
					log.error("", e);
				} finally {
					synfeed = null;
				}
			}
			sei = null;
			sci = null;	
	    } catch (Exception e) {
			log.error("", e);
		}		
	    log.info("------------- HoroscopeParser Task FINISHED -------------");
	}

	private String parseHoroscope(String strRawData) {
		int beginIndex = 0;
	    int endIndex = 0;	    
	    try {
	    	beginIndex = strRawData.indexOf("<p>");
	        endIndex = strRawData.indexOf("</p>");
	        if(beginIndex < 0 || endIndex < 0) {
	        	return strRawData;
	        }
	        else if(beginIndex < endIndex) {
	        	return strRawData.substring(beginIndex+3, endIndex);
	        }
	        else {
	        	return "";
	        }
	    }
	    catch(Exception ex) {
	        return "Currently not available";
	    }	    
	}
	
	public static void main(String[] args) {
		try {
			new HoroscopeParser().run();
		} catch (Exception e) {
			log.error("", e);
		}
	}
	
}
